import Vue from "vue";
import Router from "vue-router";
import $store from "../store";
import toLogin from "@libs/login";
import i18n from "@plugins/i18n";
import cookie from "@utils/store/cookie";


Vue.use(Router);

const router = new Router({
    mode: "history", routes: [{
        path: "/", name: "Index", meta: {
            title: "首页",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: true,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/Index.vue"], resolve)
    }, {
        path: "/agree", name: "Agree", meta: {
            title: "开户协议",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/Agree.vue"], resolve)
    }, {
        path: "/serviceOnline", name: "ServiceOnline", meta: {
            title: "客服",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/ServiceOnline.vue"], resolve)
    }, {
        path: "/login", name: "Login", meta: {
            title: "登录",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/Login.vue"], resolve)
    }, {
        path: "/register", name: "Register", meta: {
            title: "注册",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/Register.vue"], resolve)
    }, {
        path: "/user/", name: "Mine", meta: {
            title: "用户",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: true,
            navigation: true,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/Index.vue"], resolve)
    }, {
        path: "/user/setting", name: "Setting", meta: {
            title: "系统设置",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/Setting.vue"], resolve)
    }, {
        path: "/user/edit", name: "EditUser", meta: {
            title: "编辑用户",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/Edit.vue"], resolve)
    }, {
        path: "/user/editPassword", name: "EditPassword", meta: {
            title: "修改密码",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/EditPassword.vue"], resolve)
    }, {
        path: "/user/capital", name: "Capital", meta: {
            title: "修改资金密码",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/Capital.vue"], resolve)
    },{
        path: "/user/information", name: "Information", meta: {
            title: "基本信息",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/Information.vue"], resolve)
    }, {
        path: "/user/bindCard", name: "BindCard", meta: {
            title: "绑定银行卡",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/BindCard.vue"], resolve)
    }, {
        path: "/user/cardList", name: "CardList", meta: {
            title: "银行卡管理",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/CardList.vue"], resolve)
    }, {
        path: "/user/cardDetail", name: "CardDetail", meta: {
            title: "银行卡管理",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/CardInfo.vue"], resolve)
    }, {
        path: "/user/levelCenter", name: "LevelCenter", meta: {
            title: "等级中心",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/LevelCenter.vue"], resolve)
    }, {
        path: "/user/message", name: "Message", meta: {
            title: "我的消息",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/Message.vue"], resolve)
    }, {
        path: "/user/withdraw", name: "Withdraw", meta: {
            title: "兑现积分",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/Withdraw.vue"], resolve)
    }, {
        path: "/user/accountDetailList", name: "AccountDetailList", meta: {
            title: "积分明细",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/AccountDetailList.vue"], resolve)
    }, {
        path: "/user/recordDetail", name: "RecordDetail", meta: {
            title: "打赏详情",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/RecordDetail.vue"], resolve)
    }, {
        path: "/user/recordList", name: "RecordList", meta: {
            title: "打赏记录",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/RecordList.vue"], resolve)
    }, {
        path: "/user/withdrawRecord", name: "WithdrawRecord", meta: {
            title: "兑换记录",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/WithdrawRecord.vue"], resolve)
    }, {
        path: "/user/withdrawDetail", name: "WithdrawDetail", meta: {
            title: "兑换详情",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/WithdrawDetail.vue"], resolve)
    }, {
        path: "/user/sign", name: "Sign", meta: {
            title: "签到",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/user/Sign.vue"], resolve)
    }, {
        path: "/agent/index", name: "AgentCenter", meta: {
            title: "援助交际",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/agent/Index.vue"], resolve)
    }, {
        path: "/agent/report", name: "AgentReport", meta: {
            title: "团队报表",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/agent/Report.vue"], resolve)
    }, {
        path: "/agent/betList", name: "BetList", meta: {
            title: "打赏记录",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/agent/BetList.vue"], resolve)
    }, {
        path: "/agent/transactionList", name: "TransactionList", meta: {
            title: "交易明细",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/agent/TransactionList.vue"], resolve)
    }, {
        path: "/agent/inviteManager", name: "InviteManager", meta: {
            title: "新增邀请码",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/agent/InviteManager.vue"], resolve)
    }, {
        path: "/agent/QRCode", name: "QRCode", meta: {
            title: "新增邀请码",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/agent/QRCode.vue"], resolve)
    }, {
        path: "/liveReward", name: "LiveReward", meta: {
            title: "打赏记录",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/LiveReward.vue"], resolve)
    }, {
        path: "/agent/invite", name: "Invite", meta: {
            title: "推广管理",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/agent/Invite.vue"], resolve)
    }, {
        path: "/agent/member", name: "AgentMember", meta: {
            title: "会员管理",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/agent/Member.vue"], resolve)
    }, {
        path: "/welfare", name: "Welfare", meta: {
            title: "福利",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: true,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/Welfare.vue"], resolve)
    }, {
        path: "/live", name: "Live", meta: {
            title: "直播间",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: true,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/live/Index.vue"], resolve)
    }, {
        path: "/live/video", name: "LiveVideo", meta: {
            title: "视频",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["@views/live/Video.vue"], resolve)
    }, {
        path: "/movie", name: "Movie", meta: {
            title: "会员视频",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: true,
            backgroundColor: "#fff"
        }, component: resolve => require(["@views/movie/Index.vue"], resolve)
    }, {
        path: "/movie/detail", name: "MovieDetail", meta: {
            title: "视频详情",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: true,
            backgroundColor: "#fff"
        }, component: resolve => require(["@views/movie/Detail.vue"], resolve)
    }, {
        path: "/fangpian", name: "FangPian", meta: {
            title: "防骗指南",
            keepAlive: true,
            footer: true,
            header: true,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["@views/FangPian.vue"], resolve)
    }, {
        path: "/*", name: "Page404", meta: {
            title: i18n.t("pages.err_404.title"),
            keepAlive: true,
            footer: false,
            header: false,
            needToken: false,
            navigation: false,
            backgroundColor: "#fff"
        }, component: resolve => require(["../views/error/404.vue"], resolve)
    }], scrollBehavior(to, from) {
        from.meta.scrollTop = window.scrollY;
        return {x: 0, y: to.meta.scrollTop || 0};
    }
});

const {back, replace, push} = router;

router.back = function () {
    this.isBack = true;
    back.call(router);
};
router.replace = function (...args) {
    this.isReplace = true;
    replace.call(router, ...args);
};
router.push = function (...args) {
    return push.call(router, ...args).catch(err => err);
};
router.beforeEach((to, form, next) => {
    const {
        backgroundColor, header, footer, needToken, title, navigation
    } = to.meta;
    if (needToken === true && !$store.state.user.token) {
        if (form.name === "Login") return;
        cookie.set("GOTO_NAME", to.name);
        return toLogin(true, to.name);
    }
    var browserTitle = title;
    $store.commit("changeBrowserTitle", browserTitle);
    document.title = browserTitle + " - " + decodeBase64(window.RN.title);
    $store.commit("header", header === true);
    $store.commit("footer", footer === true);
    $store.commit("navigation", navigation === true);
    $store.commit("backgroundColor", backgroundColor || "#F5F5F5");

    next();
});

export default router;
